package pers.liyan.shi.springbootdynamictable.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * 启动应用后自动打开Chrome浏览器指定url
 *
 * @author shily
 * @date 2019/6/21 13:58
 */
@Component
@EnableConfigurationProperties(ChromeProperties.class)
public class ChromeCommandRunner implements CommandLineRunner {

    private Logger logger = LoggerFactory.getLogger(ChromeCommandRunner.class);

    @Autowired
    private ChromeProperties chromeProperties;

    /**
     * 执行命令,打开chrome浏览器
     */
    @Override
    public void run(String... args) throws Exception {

        if (chromeProperties.isOpen()) {
            Runtime runtime = Runtime.getRuntime();
            String cmd = chromeProperties.getChromeExcutePath() + " " + chromeProperties.getUrl();
            logger.info("Run Command " + cmd);
            runtime.exec(cmd);
        }

    }

}
